package com.xh.bussiness.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.system.bean.request.PublicSchoolRequest;
import com.xh.bussiness.system.dao.PublicSchoolMapper;
import com.xh.bussiness.system.model.Area;
import com.xh.bussiness.system.model.PublicSchool;
import com.xh.bussiness.system.service.IAreaService;
import com.xh.bussiness.system.service.IPublicSchoolService;
import com.xh.core.bean.XhPage;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.HttpUtils;
import com.xh.core.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
public class PublicSchoolServiceImpl extends BaseServiceImpl<PublicSchoolMapper, PublicSchool> implements IPublicSchoolService {

    @Autowired
    private IAreaService areaService;

    @Override
    public XhPage queryPublicSchoolPage(XhPage page, PublicSchoolRequest bean) {
        QueryWrapper qw=new QueryWrapper<PublicSchool>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List<PublicSchool> queryPublicSchoolAll(PublicSchoolRequest bean) {
        QueryWrapper qw=new QueryWrapper<PublicSchool>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return this.dao.selectList(qw);
    }


    @Override
    public void initPublicSchool() {
        QueryWrapper<Area> areaQueryWrapper = new QueryWrapper<>();
        areaQueryWrapper.isNull("parent_id");
        List<Area> provices = areaService.selectList(areaQueryWrapper, Area.class);

        Map<String, String> param = new HashMap<>();
        for (Area province : provices) {
            List<PublicSchool> result = new ArrayList<>();

            String provinceId = province.getId();
            String proviceName = province.getName();

            QueryWrapper<Area> cityWrapper = new QueryWrapper<>();
            cityWrapper.eq("parent_id", province.getId());
            List<Area> citys = areaService.selectList(cityWrapper, Area.class);

            for (Area city : citys) {
                String cityId = city.getId();
                String cityName = city.getName();

                QueryWrapper<Area> areaWrapper = new QueryWrapper<>();
                areaWrapper.eq("parent_id", city.getId());
                List<Area> areas = areaService.selectList(areaWrapper, Area.class);

                for (Area area : areas) {
                    param.put("area_id", area.getId());
                    String publicSchoolResultStr = HttpUtils.getRequest("https://www.aixuebanban.com/v2/global/schools", param);

                    JSONObject jsonObject = JSONObject.parseObject(publicSchoolResultStr);
                    List<JSONObject> datas = (List<JSONObject>)jsonObject.get("data");
                    if (!CollectionUtils.isEmpty(datas)) {
                        for (JSONObject data : datas) {
                            PublicSchool publicSchool = new PublicSchool();
                            publicSchool.setId(data.get("id").toString());
                            publicSchool.setName(data.get("name").toString());
                            publicSchool.setProvinceId(provinceId);
                            publicSchool.setProvinceName(proviceName);
                            publicSchool.setCityId(cityId);
                            publicSchool.setCityName(cityName);
                            publicSchool.setAreaId(area.getId());
                            publicSchool.setAreaName(area.getName());

                            result.add(publicSchool);
                        }
                    }

                }
            }
            this.dao.batchInsert(result);
        }
    }


    @Override
    public void batchChangeStatus(String[] ids, Integer status) {
        List<PublicSchool> publicSchools = super.getDao().selectBatchIds(Arrays.asList(ids));
        if (!CollectionUtils.isEmpty(publicSchools)) {
            for (PublicSchool publicSchool : publicSchools) {
                publicSchool.setStatus(status);
            }
            super.batchUpdate(publicSchools);
        }
    }

    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(PublicSchoolRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        if (!StringUtils.isEmpty(bean.getProvinceId())) {
            qw.eq("province_id", bean.getProvinceId());
        }
        if (!StringUtils.isEmpty(bean.getCityId())) {
            qw.eq("city_id", bean.getCityId());
        }
        if (!StringUtils.isEmpty(bean.getAreaId())) {
            qw.eq("area_id", bean.getAreaId());
        }
        if (!StringUtils.isEmpty(bean.getName())) {
            qw.like("name", bean.getName());
        }
        return qw;
    }


}
